package com.example.delicioushunter.control.databasecontrol;

import java.io.File;
import java.util.ArrayList;

import com.example.delicioushunter.control.supportcontrol.BitmapProcess;
import com.example.delicioushunter.model.database.BookColumns;
import com.example.delicioushunter.model.object.Book;
import com.example.delicioushunter.model.object.Food;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class BookControl {
	private final static String BOOKS_TABLE = "tbl_books";

	public int addBook(Book book, SQLiteDatabase db) {
		BitmapProcess process = new BitmapProcess();
		ContentValues values = new ContentValues();
		values.put(BookColumns.TITLE, book.getBook_title());
		values.put(BookColumns.IMAGE, process.getBytes(book.getImage()));

		db.insert(BOOKS_TABLE, null, values);
		int currentId =  getLastId(db);
		db.close();
		Log.d("Database", "Add book");
		return currentId;
	}

	public void updateBook(Book book, SQLiteDatabase db) {
		BitmapProcess process = new BitmapProcess();
		ContentValues values = new ContentValues();
		if (book.getBook_title() != null)
			values.put(BookColumns.TITLE, book.getBook_title());
		if (book.getImage() != null)
			values.put(BookColumns.IMAGE, process.getBytes(book.getImage()));
		db.update(BOOKS_TABLE, values, BookColumns.ID + " = ?",
				new String[] { String.valueOf(book.get_id()) });
		db.close();
	}

	public ArrayList<Book> getAllBooks(SQLiteDatabase db) {
		ArrayList<Book> books = new ArrayList<Book>();
		String selectQuery = "SELECT  * FROM " + BOOKS_TABLE;
		Cursor cursor = db.rawQuery(selectQuery, null);
		BitmapProcess process = new BitmapProcess();
		if (cursor.moveToFirst()) {
			do {
				Book book = new Book();
				book.set_id(Integer.parseInt(cursor.getString(0)));
				book.setBook_title(cursor.getString(1));
				book.setImage(process.getImage(cursor.getBlob(2)));
				book.setEmpty(false);

				books.add(book);
			} while (cursor.moveToNext());
		}
		db.close();
		Log.d("Database", "Get all books");
		return books;
	}

	public Book getBook(int _id, SQLiteDatabase db) {
		BitmapProcess process = new BitmapProcess();
		Cursor cursor = db.query(BOOKS_TABLE, new String[] { BookColumns.ID,
				BookColumns.TITLE, BookColumns.IMAGE }, BookColumns.ID + "=?",
				new String[] { String.valueOf(_id) }, null, null, null, null);
		if (cursor != null)
			cursor.moveToFirst();

		Book book = new Book();
		book.set_id(Integer.parseInt(cursor.getString(0)));
		book.setBook_title(cursor.getString(1));
		book.setImage(process.getImage(cursor.getBlob(2)));
		book.setEmpty(false);
		db.close();
		return book;
	}

	public void deleteBook(int _id, SQLiteDatabase db) {
		db.delete(BOOKS_TABLE, BookColumns.ID + " = ?",
				new String[] { String.valueOf(_id) });
		db.close();
	}

	public int getLastId(SQLiteDatabase db) {
		String query = "SELECT ROWID from " + BOOKS_TABLE
				+ " order by ROWID DESC limit 1";
		Cursor c = db.rawQuery(query, null);
		int lastId = 0;
		if (c != null && c.moveToFirst()) {
			lastId = (int) c.getLong(0);
		}
		return lastId;
	}
}
